package com.asiainfo.ssm.execute.dao.impl;

import java.util.HashMap;

import com.asiainfo.appframe.ext.flyingserver.org.apache.commons.lang.StringUtils;
import com.asiainfo.ssm.execute.bo.BOExecAdvMaterialEngine;
import com.asiainfo.ssm.execute.dao.interfaces.IExecAdvMaterialDAO;
import com.asiainfo.ssm.execute.ivalues.IBOExecAdvMaterialValue;

public class ExecAdvMaterialDAOImpl implements IExecAdvMaterialDAO {

	@Override
	public IBOExecAdvMaterialValue[] getExecAdvMaterials(String actId) throws Exception {
		String sql = "SELECT * FROM EXEC_ADV_MATERIAL A, EXEC_INSTRUCTION B WHERE A.ACT_ID = B.ACT_ID AND A.STATE = 1 AND B.ACT_STATUS <> 4 AND B.ACT_START_DATE < SYSDATE AND B.ACT_END_DATE > SYSDATE";

		HashMap<String, String> param = new HashMap<String, String>();
		if (StringUtils.isNotBlank(actId)) {
			sql += " and B.ACT_ID = :actId";
			param.put("actId", actId);
		}
		IBOExecAdvMaterialValue[] beans = BOExecAdvMaterialEngine.getBeansFromSql(sql, param);

		return beans;
	}

	@Override
	public void save(IBOExecAdvMaterialValue[] beans) throws Exception {
		for (IBOExecAdvMaterialValue iboExecAdvMaterialValue : beans) {
			if (iboExecAdvMaterialValue.isNew() || iboExecAdvMaterialValue.getMatId() == 0) {
				iboExecAdvMaterialValue.setMatId(BOExecAdvMaterialEngine.getNewId().longValue());
			}
		}
		BOExecAdvMaterialEngine.save(beans);
	}

	@Override
	public void delAdvMaterialByActId(String actId) throws Exception {
		String sql = " STATE =1 AND ACT_ID=:actId";
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("actId", actId);
		IBOExecAdvMaterialValue[] beans = BOExecAdvMaterialEngine.getBeans(sql, param);
		if (beans != null && beans.length > 0) {
			for (IBOExecAdvMaterialValue iboExecAdvMaterialValue : beans) {
				iboExecAdvMaterialValue.delete();
			}

			BOExecAdvMaterialEngine.save(beans);
		}
	}

}
